package com.ssy.lingxi.pay.serviceimpl.eAccount;

import com.ssy.lingxi.pay.common.TradeStatusEnum;
import com.ssy.lingxi.pay.entity.eAccount.EAccountTradeRecord;
import com.ssy.lingxi.pay.repository.eAccount.EAccountTradeRecordRepository;
import com.ssy.lingxi.pay.service.eAccount.IEAccountTradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员资金账户
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/12/08
 */
@Service
public class EAccountTradeServiceImpl implements IEAccountTradeService {

    @Resource
    private EAccountTradeRecordRepository eAccountTradeRecordRepository;

    /**
     * 新增e账户交易记录
     */
    @Override
    public void saveEAccountTradeRecord(EAccountTradeRecord eAccountTradeRecord) {
        eAccountTradeRecordRepository.saveAndFlush(eAccountTradeRecord);
    }

    /**
     * 充值回调
     * @param out_trade_no 交易单号
     * @param trade_no 支付平台订单号
     */
    @Override
    public void rechargeNotify(String out_trade_no, String trade_no) {
        EAccountTradeRecord eAccountTradeRecord = eAccountTradeRecordRepository.findFirstByTradeCode(out_trade_no);
        if (eAccountTradeRecord != null) {
            //更新交易记录
            eAccountTradeRecord.setStatus(TradeStatusEnum.ConfirmAccount.getCode());
            eAccountTradeRecord.setOrderCode(trade_no);
            eAccountTradeRecordRepository.saveAndFlush(eAccountTradeRecord);
        }
    }

    /**
     * 提现回调
     * @param out_trade_no 交易单号
     * @param trade_no 支付平台订单号
     */
    @Override
    public void cashOutNotify(String out_trade_no, String trade_no) {
        EAccountTradeRecord eAccountTradeRecord = eAccountTradeRecordRepository.findFirstByTradeCode(out_trade_no);
        if (eAccountTradeRecord != null) {
            //更新交易记录
            eAccountTradeRecord.setStatus(TradeStatusEnum.CashOutSuccess.getCode());
            eAccountTradeRecordRepository.saveAndFlush(eAccountTradeRecord);
        }
    }

    /**
     * 查询是否充值成功
     * @param tradeCode 交易单号
     * @return 是-true，否-false
     */
    @Override
    public Boolean getRechargeResult(String tradeCode) {
        EAccountTradeRecord eAccountTradeRecord = eAccountTradeRecordRepository.findFirstByTradeCode(tradeCode);
        return eAccountTradeRecord != null && TradeStatusEnum.ConfirmAccount.getCode().equals(eAccountTradeRecord.getStatus());
    }
}

